package com.achievement.project.system.student.service.impl;

import java.util.List;

import com.achievement.common.exception.BusinessException;
import com.achievement.common.utils.StringUtils;
import com.achievement.common.utils.security.ShiroUtils;
import com.achievement.project.system.config.service.IConfigService;
import com.achievement.project.system.course.domain.Course;
import com.achievement.project.system.user.domain.User;
import com.achievement.project.system.user.service.UserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.achievement.project.system.student.mapper.StudentMapper;
import com.achievement.project.system.student.domain.Student;
import com.achievement.project.system.student.service.IStudentService;
import com.achievement.common.utils.text.Convert;

/**
 * 学生信息Service业务层处理
 *
 * @author zkai
 * @date 2021-04-24
 */
@Service
public class StudentServiceImpl implements IStudentService
{
    private static final Logger log = LoggerFactory.getLogger(StudentServiceImpl.class);
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private IConfigService configService;

    /**
     * 查询学生信息
     *
     * @param id 学生信息ID
     * @return 学生信息
     */
    @Override
    public Student selectStudentById(Long id)
    {
        return studentMapper.selectStudentById(id);
    }

    @Override
    public List<Course> selectCourseBySid(Long sId) {
        return studentMapper.selectCourseBySid(sId);
    }

    @Override
    public int insertCourse(Long sId, Long courseId, Long cTypeId) {
        return studentMapper.insertCourse( sId, courseId, cTypeId);
    }

    /**
     * 导入用户数据
     *
     * @param studentList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importUser(List<Student> studentList, Boolean isUpdateSupport)
    {
        if (StringUtils.isNull(studentList) || studentList.size() == 0)
        {
            throw new BusinessException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Student student : studentList)
        {
            try
            {
                // 验证是否存在这个用户
                Student u = studentMapper.selectStudentByNo(student);
                if (StringUtils.isNull(u))
                {
//                    user.setPassword(password);
//                    user.setCreateBy(operName);
//                    this.insertCourse(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + student.getNo() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
//                    user.setUpdateBy(operName);
//                    this.updateUser(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " +student.getNo() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、学号 " + student.getNo() + " 课程已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + student.getNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 查询学生信息列表
     *
     * @param student 学生信息
     * @return 学生信息
     */
    @Override
    public List<Student> selectStudentList(Student student)
    {
        return studentMapper.selectStudentList(student);
    }

    /**
     * 新增学生信息
     *
     * @param student 学生信息
     * @return 结果
     */
    @Override
    public int insertStudent(Student student)
    {
        return studentMapper.insertStudent(student);
    }

    /**
     * 修改学生信息
     *
     * @param student 学生信息
     * @return 结果
     */
    @Override
    public int updateStudent(Student student)
    {
        return studentMapper.updateStudent(student);
    }

    /**
     * 删除学生信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteStudentByIds(String ids)
    {
        return studentMapper.deleteStudentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学生信息信息
     *
     * @param id 学生信息ID
     * @return 结果
     */
    @Override
    public int deleteStudentById(Long id)
    {
        return studentMapper.deleteStudentById(id);
    }

    @Override
    public int checkNoIsOnly(String number) {
        return studentMapper.checkNoIsOnly(number);
    }


    @Override
    public List<Student> selectStudentListByCid(Integer courseId,String no,String name) {
        return studentMapper.selectStudentListByCid(courseId,no,name);
    }
}
